<?php
/**
 * 首页展示
 * 作者：MYL
 */
namespace app\api\controller;

use think\Db;

class Index extends Base
{
    /**
     * 轮播图展示
     */
    public function sowingMap()
    {
        $row = Db::name('ad')->where('pid=1')->select();
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $row]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'result' => []]);
        }
    }

    /**
     * 商品分类标签
     */
    public function category_label()
    {
        $label = Db::name('goods_category_label')->select();
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'label' => $label]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'label' => []]);
        }
    }

    /**
     * 置顶分类
     */
    public function Top()
    {
        $row = Db::name('goods_category')->where('is_home=1')->limit(2)->select();
        foreach ($row as $key =>$val) {
            $goods = Db::name('goods')->field('goods_id')->where('cat_id',$row['id'])->find();
            if(!empty($goods['good_id'])) {
                $row[$key]['goods_id'] = $goods['good_id'];
            } else {
                $row[$key]['goods_id'] = '';
            }
        }
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $row]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'result' => []]);
        }
    }

    /**
     * 主推产品
     */
    public function mainPush()
    {
        $main = Db::name('goods_category')->where('id=1')->find();
        $row = Db::name('goods')->where('cat_id=1')->find();
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'goods' => $row, 'main'=>$main]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'goods' => [], 'main'=>[]]);
        }
    }

    /**
     * 特色产品
     */
    public function characteristic()
    {
        $char = Db::name('goods_category')->where('id=2')->find();
        $row = Db::name('goods_category')->where('parent_id=2')->limit(3)->select();
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $row, 'char'=>$char]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'result' => [], 'hot'=>[]]);
        }
    }

    /**
     * 热门的产品
     */
    public function hotProducts()
    {
        $hot = Db::name('goods_category')->where('id=3')->find();
        $row = Db::name('goods_category')->where('parent_id=3')->limit(4)->select();
        if (!empty($row)) {
            ajaxReturn(['status' => 1, 'msg' => '获取成功！', 'result' => $row, 'hot'=>$hot]);
        } else {
            ajaxReturn(['status' => -1, 'msg' => '获取失败！', 'result' => [], 'hot'=>[]]);
        }
    }
}